<?php
namespace My\Controller;

use Utuo\BackgroundController;

class CartTeamController extends BackgroundController {
	
	protected  $verify = array(
			'0' => "未审核",
			'1' => "审核通过",
			'3' => "审核未通过",
	);
	
	
	public function _initialize() {
		$this->setControllerTitle("审车团管理");
		$this->setActionTitle(array(
				'index' => '审车团列表',
				'add' => '审车团添加',
				'modify' => '修改审车团',
				'userList' => '用户列表',
				'recycle' => '回收站'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
	
	public function indexAction() {
		$this->getLists( 0 );
	}
	
	public function recycleAction() {
		$this->getLists( 1 );
	}
	
	public function addAction() {
		C('TOKEN_ON', true);
		$this->display("save");
	}
	
	public function modifyAction() {
		$id = I("id", 0, "intval");
		$cartTeam = M("CartTeam")->where("cid=%d and id=%d", $this->aid, $id)->find();
		$this->assign("cartTeam", $cartTeam);
		$this->display("save");
	}
	
	// 保存
	public function saveAction() {
		$d = D("CartTeam");
		$d->submit() !== false and $this->ajaxSuccess();
		$this->ajaxError($d->getError());	
	}
	
	public function getLists( $is_deleted ) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$pid = I('pid', 0, 'intval');
		$cartTeamLists = M('CartTeam')->where("cid=%d and is_deleted=%d", $this->aid, $is_deleted)->limit($pagesize)->page($page)->order("id desc")->select();
		$cartTeamCount = M('CartTeam')->where("cid=%d and is_deleted=%d", $this->aid, $is_deleted)->count();
		$this->assign('cartTeamLists', $cartTeamLists);
		$this->showPage($cartTeamCount, $pagesize);
		$this->display();
	}
	
	public function userListAction() {
		$ac_id = I("ac_id", 0, "intval");
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$pid = I('pid', 0, 'intval');
		$cartTeamUserLists = M('CartTeamUser')->where("ac_id=%d", $ac_id)->limit($pagesize)->page($page)->order("id desc")->select();
		$cartTeamUserCount = M('CartTeamUser')->where("ac_id=%d", $ac_id)->count();
		foreach ($cartTeamUserLists as &$v) {
			$v['verifyOptions'] = $this->getOption($this->verify, $v['verify'], null);
		}
		$this->assign('cartTeamUserLists', $cartTeamUserLists);
		$this->showPage($cartTeamUserCount, $pagesize);
		$this->display();
	}
	
	public function removeAction() {
		$this->ajaxRemove();
	}
	
	public function restoreAction() {
		$this->ajaxRestore();
	}
	
	// 设置审核状态
	public function setVerifyStatusAction() {
		$id = I('id',0, 'intval');
		$verify = I('verify', 0, 'intval');
		false !== M("CartTeamUser")->where(array('id'=>$id))->setField('verify', $verify) and $this->ajaxSuccess("设置成功 ");
		$this->ajaxSuccess("设置失败");
	}
	
	
}

